package com.dmwork.service.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.service.message.entity.SystemMessage;
import com.dmwork.service.message.mapper.SystemMessageMapper;
import com.dmwork.service.message.service.SystemMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统通知消息表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-06-01
 */
@Service
public class SystemMessageServiceImpl extends ServiceImpl<SystemMessageMapper, SystemMessage> implements SystemMessageService {

    @Override
    public Page<SystemMessage> getSystemMessage(String customerId, Integer page, Integer limit) {
        Page<SystemMessage> systemMessagePage = new Page<>(page, limit);
        QueryWrapper<SystemMessage> queryWrapper = new QueryWrapper<SystemMessage>().eq("customer_id", customerId);
        systemMessagePage = baseMapper.selectPage(systemMessagePage, queryWrapper);

        //开另一个线程去将当前用户的系统消息置为已读
        new Thread(() -> {
                QueryWrapper<SystemMessage> wrapper = new QueryWrapper<SystemMessage>()
                        .eq("customer_id", customerId)
                        .eq("read_status", 0);
                SystemMessage systemMessage = new SystemMessage();
                systemMessage.setReadStatus(1);
                baseMapper.update(systemMessage, wrapper);
        }).start();

        return systemMessagePage;
    }
}
